<script setup>
import { onMounted, ref, toRef } from 'vue';
const model = defineModel();
const years = ref([' ', ' ', ' ', ' ', ' ']);
const months = ref([' ', ' ', ' ', ' ', ' ']);
const days = ref([' ', ' ', ' ', ' ', ' ']);
const props = defineProps(['isShow']);
const isShow = toRef(props, 'isShow');
const emit = defineEmits(['confirm', 'close']);
let maxYear = 0;
let maxMonth = 0;
let maxDay = 0;
//计算maxYear,maxMonth,maxDay
const calculateMax = () => {
    if (years.value[2] == nowYear && months.value[2] == nowMonth) {
        maxDay = nowDay;
    }
    else {
        maxDay = new Date(years.value[2], months.value[2] + 1, 0).getDate();
        maxMonth = 11;
    }
    if (years.value[2] == nowYear) {
        maxMonth = nowMonth;
    }
    console.log(maxYear, maxMonth, maxDay);
}
const minYear = 1970;
const minMonth = 0;
const minDay = 1;
let nowYear = 0;
let nowMonth = 0;
let nowDay = 0;
//初始化years,months,days
const init = () => {
    const date = new Date();
    maxYear = date.getFullYear();
    maxMonth = date.getMonth();
    maxDay = date.getDate();
    nowYear = date.getFullYear();
    nowMonth = date.getMonth();
    nowDay = date.getDate();

    let modelTimestamp = model.value * 1000;
    let modelDate = new Date(modelTimestamp);
    years.value[2] = modelDate.getFullYear();
    months.value[2] = modelDate.getMonth();
    days.value[2] = modelDate.getDate();
    calculateMax();
    complete();
}
onMounted(() => {
    init();
})
//补全years,months,days
const complete = () => {
    const updateValues = (currentMid, min, max) => {
        return Array.from({ length: 5 }, (_, i) => {
            const value = currentMid - 2 + i;
            return (value >= min && value <= max) ? value : ' ';
        });
    };

    // 确保中间值在合法范围内
    const clamp = (value, min, max) => Math.max(min, Math.min(value, max));

    // 处理年份（需保证中间值合法）
    const midYear = clamp(years.value[2], minYear, maxYear);
    years.value = updateValues(midYear, minYear, maxYear);

    // 处理月份（需保证中间值合法）
    const midMonth = clamp(months.value[2], minMonth, maxMonth);
    months.value = updateValues(midMonth, minMonth, maxMonth);

    // 处理日期（需保证中间值合法）
    const midDay = clamp(days.value[2], minDay, maxDay);
    days.value = updateValues(midDay, minDay, maxDay);

    //console.log('Updated:', years.value, months.value, days.value);
};
const switchYear = (i) => {
    years.value[2] = years.value[2] + i;
    //检查边界
    if (years.value[2] < minYear) {
        years.value[2] = minYear;
    } else if (years.value[2] > maxYear) {
        years.value[2] = maxYear;
    }
    calculateMax();
    complete();
}
const switchMonth = (i) => {
    months.value[2] = months.value[2] + i;
    //检查边界
    if (months.value[2] < minMonth) {
        months.value[2] = minMonth;
    } else if (months.value[2] > maxMonth) {
        months.value[2] = maxMonth;
    }
    calculateMax();
    complete();
}
const switchDay = (i) => {
    days.value[2] = days.value[2] + i;
    //检查边界
    if (days.value[2] < minDay) {
        days.value[2] = minDay;
    } else if (days.value[2] > maxDay) {
        days.value[2] = maxDay;
    }
    complete();
}
const formatMonth = (value) => {
    if (value === ' ')
        return ' ';
    return value + 1 + '月';
}
const formatYear = (value) => {
    if (value === ' ')
        return ' ';
    return value + '年';
}
const formatDay = (value) => {
    if (value === ' ')
        return ' ';
    return value + '日';
}
//处理滑动
let startY = 0;
const startScroll = (event) => {
    startY = event.touches[0].clientY;
}
const onScroll = (event, handleSwitch) => {
    const currentY = event.touches[0].clientY;
    const deltaY = currentY - startY;
    if (deltaY > 10) {
        handleSwitch(-1);
        startY = currentY;
    } else if (deltaY < -10) {
        handleSwitch(1);
        startY = currentY;
    }
    //console.log(deltaY);
}

//按钮
const close = () => {
    emit('close')
}
const confirm = () => {
    let date = new Date(years.value[2], months.value[2], days.value[2], 12, 0, 0);
    model.value = date.valueOf() / 1000;
    emit('confirm', model.value);
    emit('close');

}
</script>
<template>
    <div v-if="isShow" class="datePicker-wrapper">
        <div class="mask" @click="close"></div>
        <div class="datePicker">
            <div class="datePicker-header">
                <button @click="close" class="close">取 消</button>
                <button @click="confirm" class="confirm">确 定</button>
            </div>
            <div class="datePicker-content">
                <div class="year-list list" @touchstart.passive="startScroll"
                    @touchmove.prevent="onScroll($event, switchYear)">
                    <div id="far">
                        <div>{{ formatYear(years[0]) }}</div>
                    </div>

                    <div id="near">
                        <div>{{ formatYear(years[1]) }}</div>
                    </div>
                    <div id="mid">
                        <div>{{ formatYear(years[2]) }}</div>
                    </div>
                    <div id="near">
                        <div>{{ formatYear(years[3]) }}</div>
                    </div>
                    <div id="far">
                        <div>{{ formatYear(years[4]) }}</div>
                    </div>
                </div>
                <div class="month-list list" @touchstart.passive="startScroll"
                    @touchmove.prevent="onScroll($event, switchMonth)">
                    <div id="far">
                        <div>{{ formatMonth(months[0]) }}</div>
                    </div>
                    <div id="near">
                        <div>{{ formatMonth(months[1]) }}</div>
                    </div>
                    <div id="mid">
                        <div>{{ formatMonth(months[2]) }}</div>
                    </div>
                    <div id="near">
                        <div>{{ formatMonth(months[3]) }}</div>
                    </div>
                    <div id="far">
                        <div>{{ formatMonth(months[4]) }}</div>
                    </div>
                </div>

                <div class="day-list list" @touchstart.passive="startScroll"
                    @touchmove.prevent="onScroll($event, switchDay)">
                    <div id="far">
                        <div>{{ formatDay(days[0]) }}</div>
                    </div>
                    <div id="near">
                        <div>{{ formatDay(days[1]) }}</div>
                    </div>
                    <div id="mid">
                        <div>{{ formatDay(days[2]) }}</div>
                    </div>
                    <div id="near">
                        <div>{{ formatDay(days[3]) }}</div>
                    </div>
                    <div id="far">
                        <div>{{ formatDay(days[4]) }}</div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</template>
<style>
.datePicker-wrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 999;
}

.datePicker {
    z-index: 1000;
    width: 100vw;
    height: 50vh;
    background-color: white;
    position: fixed;
    bottom: 0;
    left: 0;
    display: flex;
    flex-direction: column;
    touch-action: none;
    border-top: 5px solid #00BFFF;
    border-radius: 20px 20px 0 0;
}

.datePicker-header {
    display: flex;
    justify-content: space-between;
    padding: 10px;

    button {
        border: none;
        border-radius: 5px;
        font-size: 23px;
        color: white;
        padding: 5px;
        font-weight: 500;
    }

    .close {
        background-color: tomato;
    }

    .confirm {
        background-color: #4CAF50;
    }
}

.datePicker-content {
    display: flex;
    justify-content: space-around;
    flex-direction: row;
    margin-bottom: 0;
    flex: 1;
}

.datePicker-content div {
    display: flex;
    flex-direction: column;
    margin-bottom: 0;
    justify-content: space-between;
    flex: 1;
}

.datePicker-content .list {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.datePicker-content .list div {
    line-height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}


.datePicker-content .list {
    #mid div {
        font-size: 30px;
        border-top: 2px solid #4CAF50;
        border-bottom: 2px solid #4CAF50;
        color: black;
        font-weight: bold;
    }

    #near div {
        font-size: 25px;
        color: #696969;
    }

    #far div {
        font-size: 20px;
        color: #808080;
    }
}
</style>